﻿using SimpleConnection_UDP_WIN.extension_class.utils;
using SimpleConnection_UDP_WIN.interface_class;
using SimpleConnection_UDP_WIN.utils;
using SimpleConnection_UDP_WIN.utils.UDP;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace SimpleConnection_UDP_WIN.extension_class.hidden
{
    internal class ShutdownClass : PluginAbstractClass, PluginBaseInfoInterface
    {

        //传入命令的关键协议名
        public const string Protocol = "shutdown";

        public ShutdownClass()
        {
        }

        public ShutdownClass(string message, string ip, int port) : base(message, ip, port)
        {
            this.message = message;
            this.ip = ip;
            this.port = port;
        }

        public string Name => "电源类";
        public string Describe => "对计算机的电源进行操作的实现类";
        public string Version => "1.0.0";
        public string Help => "\npower<key>p</key>关机\n" +
     "power<key>r</key>重启\n" +
     "power<key>i</key>注销\n" +
     "power<key>l</key>锁屏\n" +
     "示例:shutdown://power<key>p</key>";

        /// <summary>
        /// 主要执行方法
        /// </summary>
        public void exec()
        {
            //判断是否验证了密码
            if (MessageParsingClass.isAdmin(message))
            {
                string text = MessageParsingClass.getText(message);

                if (text.Equals("power"))
                {

                    string key = MessageParsingClass.getKey(message);

                    new UdpTool(ip, 8871).SendMessage(GetType().Name, $"执行了 {key} 命令");
                    SoundUtil.playReminderTone();

                    switch (key)
                    {
                        case "p":
                            //关机
                            Process.Start("shutdown", "/s /t 0");
                            break;
                        case "r":
                            //重启
                            Process.Start("shutdown", "/r /t 0");
                            break;
                        case "i":
                            //注销
                            Process.Start("shutdown", "/l");
                            break;
                        case "l":
                            //锁屏
                            LockWorkStation();
                            break;
                        default:
                            //未匹配
                            break;
                    }


                }

                
            }
        }

        [DllImport("user32.dll")]
        public static extern bool ExitWindowsEx(uint uFlags, uint dwReason);
        [DllImport("user32.dll")]
        public static extern void LockWorkStation();
    }
}
